var desktop = new core.Desktop({
});
document.getElementById('desktop').appendChild(desktop.div);
core.desktop = desktop;
var toolbar = new core.Tabs({
	toolbar: true
});

for(var i = 0; i<5; i++){
	toolbar.addTab(new core.form.Button({
		text: 'Button'+i,
        eventHandlers:{
            event: 'press',
            handler: function(){
                p2.unMarkLoading();
                tb.unMarkLoading();
            }
        }
	}));
};

var tb = new core.form.Button({
	icon: 'im/add.png',
	eventHandlers:{
		event: 'press',
		handler: function(){
            p2.markLoading();
            this.markLoading();
		}
	}
});

toolbar.addTab(tb);

//for(var i = 0; i<5; i++){
//	toolbar.addTab(new core.form.Button({
//		text: 'Button'+i
//	}));
//};
//
var p = new core.ws.Frame({
	title: 'Super form',
	toolbar: toolbar,
	columns: '0.2, 0.8',
	rows: 'd, d, 1, d'
});

p.getBody().addChild(new core.Label({
	text: 'Login name:'
}));

p.getBody().addChild(new core.form.TextField({
	text: 'Login name:'
}));

p.getBody().addChild(new core.Label({
	text: 'Password:'
}));

p.getBody().addChild(new core.form.TextField({
	text: 'Login name:'
}));

p.getBody().addChild(new core.Label({
	text: 'Text:'
}));

p.getBody().addChild(new core.form.TextArea({
	text: 'Login name:'
}));

p.getBody().addChild(new core.Box({
	colspan: 2,
	columns: '1, 1',
	items: [
		new core.form.Button({
			text: 'Ok',
			align: 'right',
			eventHandlers:{
				event: 'press',
				handler: function(){
					alert('Hi korea!');
				}
			}
		}),
		new core.form.Button({
			text: 'Cancel',
			align: 'left',
			eventHandlers:{
				event: 'press',
				handler: function(){
					showDlg();
				}
			}
		})
	]
}));

var btn = new core.form.Button({
	text: 'Cancel',
	icon: 'im/add.png'
});

var showDlg = function(){
    var cancelBtn = new core.form.Button({
        text: 'Cancel',
        eventHandlers:{
            event: 'press',
            handler: function(){
                desktop.closeDialog(dlg);
            }
        }
    });
	var dlg = new core.Dialog({
		columns: '1, 1',
		maxWidth: 800,
		rows: 'd, d',
		items: [
			new core.Label({
				text: 'Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet ',
				align: 'left',
				colspan: 2
			}),
			new core.form.Button({
				align: 'right',
				text: 'Ok'
			}),
			cancelBtn
		]
	});
	desktop.showDialog(dlg);
    cancelBtn.focus();
}

btn.on('press', function(i, e){
});

// var fh0 = new core.ws.FrameHolder({
// 	
// });
// 
// var s2 = new core.SplittedPanel({
// 	rightElement: fh0,
// 	vertical: true,
// 	divider: 0.2
// });
// 
// //btn.setDisabled(true);
// //desktop.addChild(b);
// 
// //desktop.addChild(s2);
// 
// fh0.addFrame(p);
// 
// desktop.addChild(new Label({
// 	rowspan: 2,
// 	text: 'Label test#2',
// 	align: 'center',
// 	valign: 'middle'
// }));
// 
// desktop.addChild(new Label({
// 	colspan: 2,
// 	text: 'Label test',
// 	align: 'center',
// 	valign: 'bottom'
// }));
// 
// var tabs = new core.Tabs({
// //	colspan: 2
// });
// var t = null;
// for(var i = 0; i<3; i++){
// 	t = new core.Tab({
// 		text: 'Tab#'+i,
// 		icon: 'im/accept.png'
// 	});
// 	tabs.addTab(t);	
// }
// tabs.addTab(new core.Tab({
// 	text: 'Tab# Tab# Tab# Tab# Tab# Tab# Tab# Tab# Tab# Tab# Tab# Tab#',
// 	icon: ['im/application.png', 'im/accept.png']
// }));	
// 
// tabs.selectTab(t);
// 
//desktop.addChild(tabs);


var ws = new core.ws.Workspace({
	name: 'ws0'
});

p.updateLayout();
p.parentWorkspace = ws;

var tree = new core.tree.TreePanel({

});

var p2 = new core.ws.Frame({
    title: 'Another form',
    parentWorkspace: ws,
    columns: '1',
    rows: '1',
    items: [tree]
});

var ws2 = new core.ws.Workspace({
	name: 'ws1'
});
desktop.addWorkspace(ws);
desktop.addWorkspace(ws2);


ws.getFrameHolder('ws0-root').addFrame(p2);
ws.getFrameHolder('ws0-root').addFrame(p);
//ws2.getFrameHolder('fh43').addFrame(p);

desktop.updateLayout();
desktop.showWorkspace(ws);

tree.root.addChild(new core.tree.TreeItem({
    text: 'Item01'
})).addChild(new core.tree.TreeItem({
    text: 'Item02',
    type: 1
}));
tree.root.addChild(new core.tree.TreeItem({
    text: 'Item03',
    icons: 'im/add.png',
    collapsed: true,
    type: 2
})).on('load', function(i, e, item){
    p.markLoading();
    core.setTimeout(item, function(){
        this.addChild(new core.tree.TreeItem({
            text: 'Loaded item Loaded item Loaded item Loaded item Loaded item Loaded item Loaded item',
            icons: 'im/add.png',
            type: 1
        }));
        this.loadComplete();
        p.unMarkLoading();
    }, 1000);
});

for(var i = 0; i<10; i++){
    var ch = tree.root.addChild(new core.tree.TreeItem({
        text: 'Item '+i,
        icons: 'im/add.png',
        type: 0
    }));
    ch.addChild(new core.tree.TreeItem({
        text: 'Sub item 1 - '+i,
        type: 1
    }));
    ch.addChild(new core.tree.TreeItem({
        text: 'Sub item 2 - '+i,
        type: 1
    }));
}

var refreshDesktop = function(){
//	alert('refreshD: '+$("#desktop").width()+'x'+$("#desktop").height()+', '+$(document.body).width()+'x'+$(document.body).height());
	desktop.doResize(0, 0, $("#desktop").width(), $("#desktop").height());
};

refreshDesktop();
$(window).resize(function(){
	refreshDesktop();
});
